global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/family_timeseries.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

******************************************************************************
* for each patent family, gives us an overview about all relevant attributes *
******************************************************************************


qui do ${code_dir}/config/country_list.do
global countries $countrylist1995
global lim_countries US DE FR GB JP KR CN


*start out at application level
use ${commondata_dir}/patstat_2018b/appln_info.dta, clear

keep appln_id appln_auth
do ${code_dir}/config/auth_map.do appln_auth

*import different technologies, including pharma and chemistry for stats
foreach xxx in auto90 auto95 pphar60 pphar90 pphar95 pauto60 pauto90 pauto95 pauto90_rm6 pchem60 pchem90 pchem95 robo80 robo90 robo95 autm80 CNC80 {
	mmerge appln_id using ${dataset_dir}/patent_list/`xxx'_patents.dta, unmatched(master)
	gen `xxx'=(_m==3)
}


*merge in machinery and mark it
mmerge appln_id using ${dataset_dir}/patent_list/pats_tfa.dta, unmatched(master)
gen pat_relevant_field = _m == 3
drop _m

*move to the family level
mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year)
ren fam_earliest_appln_year year
keep if year < 9999
bys docdb_family_id : egen relevant_field = max(pat_relevant_field)
drop pat_relevant_field

*replace EP with country for more precision for european patents
mmerge appln_id using ${commondata_dir}/patstat_2018b/PRS_EPO_national_phase.dta, unmatched(master)
replace appln_auth = country if _m == 3
drop _m country

*unify/split different variables for patent authority
foreach ctry of global countries {
	di "auth: ctry = `ctry'"
	gen pat_auth_`ctry' = appln_auth == "`ctry'"
	bys docdb_family_id : egen auth_`ctry' = max(pat_auth_`ctry')
	drop pat_auth_`ctry'
}

drop appln_auth
duplicates drop


mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_applicant_country.dta, unmatched(master) ukeep(appl_country)
drop _m

*unify/split different variables for patent applicant for some large countries
foreach ctry of global lim_countries {
	gen pat_appl_`ctry' = appl_country == "`ctry'"
	bys docdb_family_id : egen appl_`ctry' = max(pat_appl_`ctry')
	drop pat_appl_`ctry'
}

drop appln_id appl_country
duplicates drop


*load biadic information
mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, ukeep(biadic_D) unmatched(master)
ren biadic_D biadic
drop _m

*labeling

compress
sort docdb_family_id
save ${final_dir}/family_timeseries_full.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat
